package com.suneast.suneaststore.modules.personal.info;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.suneast.suneaststore.R;
import com.suneast.suneaststore.app.AccountManager;
import com.suneast.suneaststore.modules.common.Flag;
import com.suneast.suneaststore.modules.main.BottomFragment;
import com.suneast.suneaststore.modules.personal.sign.SignInFragment;
import com.suneast.suneaststore.modules.personal.sign.SignRequestHandler;
import com.suneast.suneaststore.modules.personal.sign.event.ModifyPwdResult;
import com.suneast.suneaststore.ui.fragments.BaseFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

public class ModifyPasswordFragment extends BaseFragment {
    @Override
    public int setLayout() {
        return R.layout.fragment_modify_password;
    }

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.et_old_password)
    public EditText oldPasswordEdit;
    @BindView(R.id.et_new_password)
    public EditText newPasswordEdit;
    @BindView(R.id.et_re_input_password)
    public EditText reInputPasswordEdit;

    private String oldPwd = "";
    private String newPwd = "";
    private String reInputPwd = "";

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        EventBus.getDefault().register(this);
        toolbar.setNavigationOnClickListener(v -> pop());
        oldPasswordEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                oldPwd = s.toString();
            }
        });
        newPasswordEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                newPwd = s.toString();
            }
        });
        reInputPasswordEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                reInputPwd = s.toString();
            }
        });
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }

    @OnClick({R.id.btn_confirm_modify})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.btn_confirm_modify: {
                if (oldPwd.length() >= 6 && newPwd.length() >= 6 && reInputPwd.length() >= 6) {
                    if (newPwd.equals(reInputPwd)) {
                        SignRequestHandler.modifyPwd(oldPwd, newPwd);
                    } else {
                        Toast.makeText(_mActivity, "两次密码不一致",
                                Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(_mActivity, "请输入字母、数字混合至少6位",
                            Toast.LENGTH_SHORT).show();
                }
            }
            break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onModifyPwdResult(ModifyPwdResult result) {
        switch (result.getResult()) {
            case Flag.Result.SUCCEED: {
                AccountManager.exitCurrentAccount();
                Toast.makeText(_mActivity, "修改密码成功,请重新登录", Toast.LENGTH_SHORT).show();
                popTo(BottomFragment.class, false);
                start(new SignInFragment());
            }
            break;
            case Flag.Result.FAILURE: {
                Toast.makeText(_mActivity, "修改密码失败", Toast.LENGTH_SHORT).show();
            }
            break;
        }
    }
}
